<?php

class IndexController extends Zend_Controller_Action
{
    private $_params;
	private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();
        $this->_acl         = new My_Acl();
        $this->view->acl    = $this->_acl;
      
        $this->_auth        = $this->_acl->checkAllowed();
        $this->view->auth   = $this->_auth;
    }
    public function indexAction(){
    }
    public function updateAction(){
        $modelGame = new Default_Model_Game();
        
        $gameList = $modelGame->getAllData();
        foreach($gameList as $key => $game){
            if(!file_exists(CONFIG_PATH."/old/{$game->gameId}.ini")){
                continue;
            }
            
            $configGameOld = new Zend_Config_Ini(CONFIG_PATH."/old/{$game->gameId}.ini", APPLICATION_ENV);
            
            $configServer = new Zend_Config(array(), true);
            $configServer->production = array();
            $configServer->staging    = array();
            $configServer->local    = array();
            $configServer->setExtend('staging', 'production');
            $configServer->setExtend('local', 'production');
            
            if(isset($configGameOld->channel))
                $configServer->production->channel = $configGameOld->channel;
            if(isset($configGameOld->server))
                $configServer->production->server = $configGameOld->server;
            
            $writer = new Zend_Config_Writer_Ini(array('config'   => $configServer, 'filename' => CONFIG_PATH."/game/{$game->gameId}-channel.ini"));
            $writer->write();
            continue;
            
            $configGame = new Zend_Config(array(), true);
            $configGame->production = array();
            $configGame->staging    = array();
            $configGame->local    = array();
            $configGame->setExtend('staging', 'production');
            $configGame->setExtend('local', 'production');
                        
            $configGame->production->applications = $configGameOld->applications;
            if(isset($configGameOld->transaction)){
                $configGame->production->transaction = $configGameOld->transaction;
            }
            if(isset($configGameOld->card)){
                $configGame->production->card = $configGameOld->card;
            }
            if(isset($configGameOld->shop)){
                $configGame->production->shop = $configGameOld->shop;
            }
            if(isset($configGameOld->expense)){
                $configGame->production->expense = $configGameOld->expense;
            }
            if(isset($configGameOld->exchange)){
                $configGame->production->exchange = $configGameOld->exchange;
            }
            if(isset($configGameOld->ccu)){
                $configGame->production->ccu = $configGameOld->ccu;
            }
            if(isset($configGameOld->refrest)){
                $configGame->production->ccuRefrestTime = $configGameOld->refrest;
            }
            if(isset($configGameOld->email)){
                $configGame->production->email = $configGameOld->email;
            }
            if(isset($configGameOld->sendEmail)){
                $configGame->production->sendEmail = $configGameOld->sendEmail;
            }
            if(isset($configGameOld->cache)){
                $configGame->production->cache = $configGameOld->cache;
            }
            if(isset($configGameOld->server)){
                foreach($configGameOld->server as $key=>$value){
                    if(!isset($configGame->production->server)){
                        $configGame->production->server = array();
                    }
                    if(!isset($configGame->production->server->$key)){
                        $configGame->production->server->$key = array();
                    }
                    $configGame->production->server->$key->name = $value->name;
                    $configGame->production->server->$key->dateOpen = "";
                    $configGame->production->server->$key->dateClose = "";
                }
            }
            if(isset($configGameOld->channel)){
                foreach($configGameOld->channel as $key=>$value){
                    
                    if(!isset($configGame->production->channel)){
                        $configGame->production->channel = array();
                        $configGame->production->channel->list = array();
                        $configGame->production->channel->serverList = array();
                    }
                    if(!isset($configGame->production->channel->list->$key)){
                        $configGame->production->channel->list->$key = array();
                    }
                    if(!isset($configGame->production->channel->serverList->$key)){
                        $configGame->production->channel->serverList->$key = array();
                    }
                    $configGame->production->channel->list->$key->name = $value->name;
                    $configGame->production->channel->list->$key->dateOpen = "";
                    $configGame->production->channel->list->$key->dateClose = "";
                    $configGame->production->channel->serverList->$key->name = $value->value;
                }
            }
            $configGame->production->api = array();
            if(isset($configGameOld->kpi->PCU->index)){
                $configGame->production->api->pcu = array();
                if(isset($configGameOld->kpi->pcu->definition))
                    $configGame->production->api->pcu->definition = $configGameOld->kpi->pcu->definition;
                if(isset($configGameOld->kpi->pcu->index))
                    $configGame->production->api->pcu->ssgroup = $configGameOld->kpi->pcu->index;
                if(isset($configGameOld->kpi->channel->PCU->index))
                    $configGame->production->api->pcu->channel = $configGameOld->kpi->channel->pcu->index;
            }
            if(isset($configGameOld->kpi->PCU7->index)){
                $configGame->production->api->pcu7 = array();
                $configGame->production->api->pcu7->index = $configGameOld->kpi->PCU7->index;
            }
            if(isset($configGameOld->kpi->PCU30->index)){
                $configGame->production->api->pcu30 = array();
                $configGame->production->api->pcu30->index = $configGameOld->kpi->PCU30->index;
            }
            if(isset($configGameOld->kpi->channel->PCU->index)){
                $configGame->production->api->pcu->totalServer = $configGameOld->kpi->channel->PCU->index;
            }
            if(isset($configGameOld->kpi->channel->PCU7->index)){
                $configGame->production->api->pcu7->totalServer = $configGameOld->kpi->channel->PCU7->index;
            }
            if(isset($configGameOld->kpi->channel->PCU30->index)){
                $configGame->production->api->pcu30->totalServer = $configGameOld->kpi->channel->PCU30->index;
            }
            if(isset($configGameOld->kpi->TotalUsers)){
                $configGame->production->api->TotalUsers = array();
                if(isset($configGameOld->kpi->TotalUsers->definition))
                    $configGame->production->api->TotalUsers->definition = $configGameOld->kpi->TotalUsers->definition;
                if(isset($configGameOld->kpi->TotalUsers->SSGROUP->index))
                    $configGame->production->api->TotalUsers->ssgroup = $configGameOld->kpi->TotalUsers->SSGROUP->index;
                if(isset($configGameOld->kpi->TotalUsers->GAME5->index))
                    $configGame->production->api->TotalUsers->ssgroup = $configGameOld->kpi->TotalUsers->GAME5->index;
                if(isset($configGameOld->kpi->TotalUsers->index))
                    $configGame->production->api->TotalUsers->channel = $configGameOld->kpi->TotalUsers->index;
            }
            # register user
            if(isset($configGameOld->kpi->NewRegisteredUsers)){
                $configGame->production->api->NewRegisteredUsers = array();
                if(isset($configGameOld->kpi->NewRegisteredUsers->definition))
                    $configGame->production->api->NewRegisteredUsers->definition = $configGameOld->kpi->NewRegisteredUsers->definition;
                if(isset($configGameOld->kpi->NewRegisteredUsers->SSGROUP->index))
                    $configGame->production->api->NewRegisteredUsers->ssgroup = $configGameOld->kpi->NewRegisteredUsers->SSGROUP->index;
                if(isset($configGameOld->kpi->NewRegisteredUsers->index))
                    $configGame->production->api->NewRegisteredUsers->channel = $configGameOld->kpi->NewRegisteredUsers->index;
            }
            if(isset($configGameOld->kpi->NewRegisteredUsers7)){
                $configGame->production->api->NewRegisteredUsers7 = array();
                if(isset($configGameOld->kpi->NewRegisteredUsers7->definition))
                    $configGame->production->api->NewRegisteredUsers7->definition = $configGameOld->kpi->NewRegisteredUsers7->definition;
                if(isset($configGameOld->kpi->NewRegisteredUsers7->SSGROUP->index))
                    $configGame->production->api->NewRegisteredUsers7->ssgroup = $configGameOld->kpi->NewRegisteredUsers7->SSGROUP->index;
                if(isset($configGameOld->kpi->NewRegisteredUsers7->index))
                    $configGame->production->api->NewRegisteredUsers7->channel = $configGameOld->kpi->NewRegisteredUsers7->index;
            }
            if(isset($configGameOld->kpi->NewRegisteredUsers30)){
                $configGame->production->api->NewRegisteredUsers30 = array();
                if(isset($configGameOld->kpi->NewRegisteredUsers30->definition))
                    $configGame->production->api->NewRegisteredUsers30->definition = $configGameOld->kpi->NewRegisteredUsers30->definition;
                if(isset($configGameOld->kpi->NewRegisteredUsers30->SSGROUP->index))
                    $configGame->production->api->NewRegisteredUsers30->ssgroup = $configGameOld->kpi->NewRegisteredUsers30->SSGROUP->index;
                if(isset($configGameOld->kpi->NewRegisteredUsers30->index))
                    $configGame->production->api->NewRegisteredUsers30->channel = $configGameOld->kpi->NewRegisteredUsers30->index;
            }
            # active user
            if(isset($configGameOld->kpi->ActiveUsers)){
                $configGame->production->api->ActiveUsers = array();
                if(isset($configGameOld->kpi->ActiveUsers->definition))
                    $configGame->production->api->ActiveUsers->definition = $configGameOld->kpi->ActiveUsers->definition;
                if(isset($configGameOld->kpi->ActiveUsers->SSGROUP->index))
                    $configGame->production->api->ActiveUsers->ssgroup = $configGameOld->kpi->ActiveUsers->SSGROUP->index;
                if(isset($configGameOld->kpi->ActiveUsers->index))
                    $configGame->production->api->ActiveUsers->channel = $configGameOld->kpi->ActiveUsers->index;
            }
            if(isset($configGameOld->kpi->ActiveUsers30)){
                $configGame->production->api->ActiveUsers30 = array();
                if(isset($configGameOld->kpi->ActiveUsers30->definition))
                    $configGame->production->api->ActiveUsers30->definition = $configGameOld->kpi->ActiveUsers30->definition;
                if(isset($configGameOld->kpi->ActiveUsers30->SSGROUP->index))
                    $configGame->production->api->ActiveUsers30->ssgroup = $configGameOld->kpi->ActiveUsers30->SSGROUP->index;
                if(isset($configGameOld->kpi->ActiveUsers30->index))
                    $configGame->production->api->ActiveUsers30->channel = $configGameOld->kpi->ActiveUsers30->index;
            }
            # register role
            if(isset($configGameOld->kpi->RegisteredRole)){
                $configGame->production->api->RegisteredRole = array();
                if(isset($configGameOld->kpi->RegisteredRole->definition))
                    $configGame->production->api->RegisteredRole->definition = $configGameOld->kpi->RegisteredRole->definition;
                if(isset($configGameOld->kpi->RegisteredRole->index))
                    $configGame->production->api->RegisteredRole->total = $configGameOld->kpi->RegisteredRole->index;
                if(isset($configGameOld->kpi->channel->RegisteredRole->index))
                    $configGame->production->api->RegisteredRole->totalServer = $configGameOld->kpi->channel->RegisteredRole->index;
            }
            # active role
            if(isset($configGameOld->kpi->ActiveRole)){
                $configGame->production->api->ActiveRole = array();
                if(isset($configGameOld->kpi->ActiveRole->definition))
                    $configGame->production->api->ActiveRole->definition = $configGameOld->kpi->ActiveRole->definition;
                if(isset($configGameOld->kpi->ActiveRole->index))
                    $configGame->production->api->ActiveRole->total = $configGameOld->kpi->ActiveRole->index;
                if(isset($configGameOld->kpi->channel->ActiveRole->index))
                    $configGame->production->api->ActiveRole->totalServer = $configGameOld->kpi->channel->ActiveRole->index;
            }
            # total convert user
            if(isset($configGameOld->kpi->ConvertedUsers)){
                $configGame->production->api->ConvertedUsers = array();
                if(isset($configGameOld->kpi->ConvertedUsers->definition))
                    $configGame->production->api->ConvertedUsers->definition = $configGameOld->kpi->ConvertedUsers->definition;
                if(isset($configGameOld->kpi->ConvertedUsers->SSGROUP->index))
                    $configGame->production->api->ConvertedUsers->ssgroup = $configGameOld->kpi->ConvertedUsers->SSGROUP->index;
                if(isset($configGameOld->kpi->ConvertedUsers->index))
                    $configGame->production->api->ConvertedUsers->channel = $configGameOld->kpi->ConvertedUsers->index;
            }
            if(isset($configGameOld->kpi->ConvertedUsers7)){
                $configGame->production->api->ConvertedUsers7 = array();
                if(isset($configGameOld->kpi->ConvertedUsers7->definition))
                    $configGame->production->api->ConvertedUsers7->definition = $configGameOld->kpi->ConvertedUsers7->definition;
                if(isset($configGameOld->kpi->ConvertedUsers7->SSGROUP->index))
                    $configGame->production->api->ConvertedUsers7->ssgroup = $configGameOld->kpi->ConvertedUsers7->SSGROUP->index;
                if(isset($configGameOld->kpi->ConvertedUsers7->index))
                    $configGame->production->api->ConvertedUsers7->channel = $configGameOld->kpi->ConvertedUsers7->index;
            }
            if(isset($configGameOld->kpi->ConvertedUsers30)){
                $configGame->production->api->ConvertedUsers30 = array();
                if(isset($configGameOld->kpi->ConvertedUsers30->definition))
                    $configGame->production->api->ConvertedUsers30->definition = $configGameOld->kpi->ConvertedUsers30->definition;
                if(isset($configGameOld->kpi->ConvertedUsers30->SSGROUP->index))
                    $configGame->production->api->ConvertedUsers30->ssgroup = $configGameOld->kpi->ConvertedUsers30->SSGROUP->index;
                if(isset($configGameOld->kpi->ConvertedUsers30->index))
                    $configGame->production->api->ConvertedUsers30->channel = $configGameOld->kpi->ConvertedUsers30->index;
            }
            # new-convert user
            if(isset($configGameOld->kpi->NewConvertedUsers)){
                $configGame->production->api->NewConvertedUsers = array();
                if(isset($configGameOld->kpi->NewConvertedUsers->definition))
                    $configGame->production->api->NewConvertedUsers->definition = $configGameOld->kpi->NewConvertedUsers->definition;
                if(isset($configGameOld->kpi->NewConvertedUsers->SSGROUP->index))
                    $configGame->production->api->NewConvertedUsers->ssgroup = $configGameOld->kpi->NewConvertedUsers->SSGROUP->index;
                if(isset($configGameOld->kpi->NewConvertedUsers->index))
                    $configGame->production->api->NewConvertedUsers->channel = $configGameOld->kpi->NewConvertedUsers->index;
            }
            # re-convert user
            if(isset($configGameOld->kpi->ReconvertedUsers)){
                $configGame->production->api->ReconvertedUsers = array();
                if(isset($configGameOld->kpi->ReconvertedUsers->definition))
                    $configGame->production->api->ReconvertedUsers->definition = $configGameOld->kpi->ReconvertedUsers->definition;
                if(isset($configGameOld->kpi->ReconvertedUsers->SSGROUP->index))
                    $configGame->production->api->ReconvertedUsers->ssgroup = $configGameOld->kpi->ReconvertedUsers->SSGROUP->index;
                if(isset($configGameOld->kpi->ReconvertedUsers->index))
                    $configGame->production->api->ReconvertedUsers->channel = $configGameOld->kpi->ReconvertedUsers->index;
            }
            # gross revenue
            if(isset($configGameOld->kpi->GrossRevenue)){
                $configGame->production->api->GrossRevenue = array();
                if(isset($configGameOld->kpi->GrossRevenue->definition))
                    $configGame->production->api->GrossRevenue->definition = $configGameOld->kpi->GrossRevenue->definition;
                if(isset($configGameOld->kpi->GrossRevenue->SSGROUP->index))
                    $configGame->production->api->GrossRevenue->ssgroup = $configGameOld->kpi->GrossRevenue->SSGROUP->index;
                if(isset($configGameOld->kpi->GrossRevenue->index))
                    $configGame->production->api->GrossRevenue->channel = $configGameOld->kpi->GrossRevenue->index;
                if(isset($configGameOld->revenue->params->serverindex))
                    $configGame->production->api->GrossRevenue->server = $configGameOld->revenue->params->serverindex;
                if(isset($configGameOld->revenue->params->serverChannel))
                    $configGame->production->api->GrossRevenue->serverChannel = $configGameOld->revenue->params->serverChannel;
                if(isset($configGameOld->revenue->params->serverChannel))
                    $configGame->production->api->GrossRevenue->serverChannel = $configGameOld->revenue->params->serverChannel;
                    
                if(isset($configGameOld->revenue->realtime->params->serverChannel))
                    $configGame->production->api->GrossRevenue->serverChannel = $configGameOld->revenue->params->serverChannel;
            }
            
            $writer = new Zend_Config_Writer_Ini(array('config'   => $configGame, 'filename' => CONFIG_PATH."/game/{$game->gameId}-api.ini"));
            $writer->write();
            /*
            if(isset($configGame->channel)){
                $stt = 1;
                foreach($configGame->channel as $key=>$value){
                    //add channel to DB
                    $dataChannel = array(
                                    'channelId'     => $key,
                                    'channelName'   => $value->name,
                                    'gameId'        => $game->gameId,
                                    'level'         => $stt
                                );
                    $modelGameChannel->addData($dataChannel);
                    //add channel server to DB
                    if(isset($value->value)){
                        foreach($value->value as $server){
                            $dataChannelServer = array(
                                    'channelId'     => $key,
                                    'serverId'      => $server,
                                    'gameId'        => $game->gameId
                                );
                            $modelGameChannelServer->addData($dataChannelServer);
                        }
                    }
                    $stt++;
                }
            }
            
            if(isset($configGame->server)){
                $stt = 1;
                foreach($configGame->server as $key=>$value){
                    //add server to DB
                    $dataServer = array(
                                    'serverId'      => $key,
                                    'serverName'    => $value->name,
                                    'gameId'        => $game->gameId,
                                    'level'         => $stt
                                );
                    $modelGameServer->addData($dataServer);
                    $stt++;
                }
            }
            
            if(isset($configGame->clusters)){
                $stt = 1;
                foreach($configGame->clusters as $key=>$value){
                    $dataCluster = array(
                                    'clusterId'     => $key,
                                    'clusterName'   => $value->name,
                                    'serverList'    => json_encode($value->value->toArray()),
                                    'type'          => 1,
                                    'gameId'        => $game->gameId,
                                    'level'         => $stt
                                );
                    $modelGameCluster->addData($dataCluster);
                    $stt++;
                }
            }
            if(isset($configGame->ccu->clusters)){
                $stt = 1;
                foreach($configGame->ccu->clusters as $key=>$value){
                    $dataCluster = array(
                                    'clusterId'     => $key,
                                    'clusterName'   => $value->name,
                                    'serverList'    => json_encode($value->value->toArray()),
                                    'type'          => 2,
                                    'gameId'        => $game->gameId,
                                    'level'         => $stt
                                );
                    $modelGameCluster->addData($dataCluster);
                    $stt++;
                }
            }
            */
        }
        exit;
    }
}